import java.io.*;
import java.util.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.openxml4j.exceptions.*;

class Main {
   public static void testCreateExcelFile() throws IOException {
      Workbook wb = new HSSFWorkbook();
      Sheet sheet = wb.createSheet("new sheet");

      CreationHelper createHelper = wb.getCreationHelper();

      Row row = sheet.createRow((short)0);
      // Create a cell and put a value in it.
      Cell cell = row.createCell(0);
      cell.setCellValue(1);

      row.createCell(1).setCellValue(1.2);
      row.createCell(2).setCellValue(
           createHelper.createRichTextString("This is a string"));
      row.createCell(3).setCellValue(true);

      FileOutputStream fileOut = new FileOutputStream("workbook.xls");
      wb.write(fileOut);
      fileOut.close();
   }
   public static void testReadExcelFile() throws Exception {
      InputStream inp = new FileInputStream("red_bible.xls");
      Workbook wb = org.apache.poi.ss.usermodel.WorkbookFactory.create(inp);
      Sheet sheet = wb.getSheetAt(0);
      final int last=sheet.getLastRowNum();
      for(int index=0; index<last; index++) {
         final Row row=sheet.getRow(index);
         final Cell word=row.getCell(0);
         if(word==null) continue;
         System.out.print(word.getStringCellValue());
         System.out.print(" ");
         final Cell meaning=row.getCell(2);
         if(meaning==null) continue;
         System.out.println(meaning.getStringCellValue());
      }
   }
   public static void testRandom() {
      final int CELLS=10;
      final int REP=10000;
      final int[] cells=new int[CELLS];
      for(int i=0; i<cells.length; i++) cells[i]=0;

      for(int i=0; i<REP; i++) {
         final double r=Math.random();
         final int ir=(int)(r*CELLS);
         cells[ir]++;
         //if(ir>=10) System.err.println(ir);
      }
      for(int i=0; i<cells.length; i++) {
         int width=(int)(((double)cells[i])/REP*20);
         int j=0;
         for(; j<width; j++) {
            System.out.print("=");
         }
         System.out.println("<"+i+":"+cells[i]);
      }
   }
   public static void testRandom2() {
      final Random ran=new Random();
      for(int i=0; i<100; i++) {
         System.out.println(ran.nextInt(100));
      }
   }

   public static void main(String[] args) throws Exception {
      //testCreateExcelFile();
      //testReadExcelFile();
      //testRandom();
      //testRandom2();
   }
}
